<?php

namespace app\common\model;

use app\common\model\account\ShopAccountModel;
use think\facade\Db;


class ShopAllianceModel extends CommonModel{

    protected  $name = 'shop_alliance';

    protected  $pk   = 'id';


    public function application()

    {
        return $this->hasOne(AppModel::class,'app_id','app_id');

    }

    public static function info($where){
        $info = Db::name('shop_alliance')->alias('a')
            ->join('shop b','a.shop_id=b.shop_id')
            ->join('shop_detail d','a.shop_id=d.shop_id')
            ->leftJoin('shop_alliance_conf c','a.shop_id=c.shop_id')
            ->leftJoin('shop_brand e','b.brand_id=e.id')
            ->where(['a.is_delete'=>0])
            ->when(!empty($where['shop_id']),function ($query) use ($where){
                $query->where(['a.shop_id'=>$where['shop_id']]);
            })
            ->when(!empty($where['id']),function ($query) use ($where){
                $query->where(['a.id'=>$where['id']]);
            })
            ->when(isset($where['status']),function ($query) use ($where){
                $query->where(['a.status'=>$where['status']]);
            })
            ->when(!empty($where['shop_name']),function ($query) use ($where){
                $query->where('b.name','like','%'.$where['shop_name'].'%');
            })
            ->when(!empty($where['parent_shop_id']),function ($query) use ($where){
                $query->where(['a.parent_shop_id'=>$where['parent_shop_id']]);
            })
            ->field("a.*,b.name as shop_name,d.logo,d.address,d.tel,b.type,c.is_shareholder,c.is_member,c.is_pushflow,e.name as brand_name,e.id as brand_id")
            ->find();
        if(!empty($info)){
            $info['end_time']='不限';
            if(!empty($info) && $info['live_num']>0){
                $s_y=date('Y',strtotime($info['start_time']));
                $s_m=date('m',strtotime($info['start_time']));
                $s_d=date('d',strtotime($info['start_time']));
                $e_y=$s_y+$info['live_num'];
                $info['end_time']=$e_y.'-'.$s_m.'-'.$s_d;
            }
            if(!empty($v['brand_id'])){
                $parent=ShopBrandModel::info(['id'=>$v['parent_id']]);
                if(!empty($parent)){
                    $v['parent_name']=$parent['name'];
                }
            }
        }
        return $info;
    }
    public static function list($where,$order,$page=1,$limit=10){
        $query = Db::name('shop_alliance')->alias('a')
            ->join('shop b','a.shop_id=b.shop_id')
            ->join('shop_detail d','a.shop_id=d.shop_id')
            ->leftJoin('shop_alliance_conf c','a.id=c.alliance_id')
            ->leftJoin('shop_brand e','b.brand_id=e.id')
//            ->leftJoin('shop_brand f','e.parent_id=f.id')
            ->where(['a.is_delete'=>0])
            ->when(!empty($where['shop_id']),function ($query) use ($where){
                $query->where(['a.shop_id'=>$where['shop_id']]);
            })
            ->when(!empty($where['shop_name']),function ($query) use ($where){
                $query->where('b.name','like','%'.$where['shop_name'].'%');
            })
            ->when(!empty($where['parent_shop_id']),function ($query) use ($where){
                $query->where(['a.parent_shop_id'=>$where['parent_shop_id']]);
            })
            ->field("a.*,b.name as shop_name,d.logo,d.address,d.tel,b.type,e.name as brand_name,e.id as brand_id,e.parent_id as parent_id");
        $count=$query->count();
        $list=$query->order($order)
            ->limit($limit)->page($page)
            ->select()
            ->toArray();
        if(!empty($list)){
            foreach ($list as &$v){
                $v['end_time']='不限';
                if($v['live_num']>0){
                    $s_y=date('Y',strtotime($v['start_time']));
                    $s_m=date('m',strtotime($v['start_time']));
                    $s_d=date('d',strtotime($v['start_time']));
                    $e_y=$s_y+$v['live_num'];
                    $v['end_time']=$e_y.'-'.$s_m.'-'.$s_d;
                }
                if($v['parent_shop_id']==0){
                    $v['iden']='盟主';
                }else{
                    $v['iden']='盟员';
                }
                if(!empty($v['parent_id'])){
                    $parent=ShopBrandModel::info(['id'=>$v['parent_id']]);
                    if(!empty($parent)){
                        $v['parent_name']=$parent['name'];
                    }
                }
            }
        }
        return compact('count','list');
    }
    public static function up($data,$where){
        $data['edit_time']=date('Y-m-d H:i:s');
        $res=self::update($data,$where);
        return $res;
    }
   public function inc($where, $field, $num)
   {
       return parent::n_inc($where, $field, $num); // TODO: Change the autogenerated stub
   }
   public function dec($where, $field, $num)
   {
       return parent::n_dec($where, $field, $num); // TODO: Change the autogenerated stub
   }
}

